package com.loong.musicdemo.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.loong.musicdemo.R;
import com.loong.musicdemo.util.FormatTimeUtil;

import java.util.List;

/**
 * 歌词适配器
 */
public class LrcAdapter extends RecyclerView.Adapter<LrcAdapter.ViewHolder> {
    private List<Pair<Long, String>> maps;
    private int highLightPosition = 0;//需要高亮显示的行

    public LrcAdapter(List<Pair<Long, String>> maps) {
        this.maps = maps;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_lrc_string, parent, false);
        return new ViewHolder(view);
    }


    /**
     * 默认绑定
     *
     * @param holder   viewholder
     * @param position 位置
     */
    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Pair<Long, String> lrc = maps.get(position);
        holder.lrcString.setText(lrc.second);
        //设置高亮行
        try {
            if (!isHighLight(position)) {
                holder.lrcString.setTextColor(Color.parseColor("#dddddd"));//先设置普同行颜色,防止直接套用
            } else {
                holder.lrcString.setTextColor(Color.WHITE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.root.setOnClickListener(v -> {
            if (onPlayClickListener != null) {
                onPlayClickListener.onclick();
            }
        });
    }

    @Override
    public int getItemCount() {
        return maps.size();
    }

    public interface OnPlayClickListener {
        /**
         * 点击播放图标
         */
        void onclick();
    }

    private OnPlayClickListener onPlayClickListener;

    public void setOnPlayClickListener(OnPlayClickListener onPlayClickListener) {
        this.onPlayClickListener = onPlayClickListener;
    }

    /**
     * 设置高亮行
     *
     * @param position 高亮行位置
     */
    public void highLightItem(int position) {//高亮显示设置
        notifyItemChanged(highLightPosition);//同步之前高亮行
        highLightPosition = position;
        notifyItemChanged(position);
    }

    /**
     * 判断是否为高两行
     *
     * @param position 当前行位置
     * @return 是否高亮
     */
    public boolean isHighLight(int position) {
        return highLightPosition == position;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout root;//容器
        TextView lrcString;//歌词字符串

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            root = itemView.findViewById(R.id.root);
            lrcString = itemView.findViewById(R.id.lrc_string);
        }
    }
}
